﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameLibrary;
using AimGameLibrary.GameObject;

namespace AimGameEngine.EditorObject
{
    using GdiRectangle = System.Drawing.Rectangle;
    public class Grid : Image2D
    {
        public GdiRectangle myArea;
        List<Image2D> Colums;
        List<Image2D> Rows;
        int LangthWidth = 100;
        int LangthHeight = 80;
        float myDepth = 1;
        Color myColorGrid = new Color(Color.Blue.R,Color.Blue.G,Color.Blue.B, 100);

        public Grid(SpriteBatch inputspritebatch, Camera inputcamera,Texture2D inputTexture)
            :base(inputspritebatch,inputcamera)
        {
            //myArea = inputArea;
            this.MyTexture2D = inputTexture;
            Colums = new List<Image2D>();
            Rows = new List<Image2D>();
        }

        public override void UpdateTime(TimeSpan timespan)
        {
            base.UpdateTime(timespan);
            #region New Colum
            Colums.Clear();

            int LoopColum = (myArea.Width / LangthWidth) + 2;
            int newPositionX = (int)((this.camera.MyViewData.Position.X * -1 )/ LangthWidth);
            int StartPositionX = newPositionX * LangthWidth;
            for (int i = 0; i < LoopColum; i++)
            {
                Image2D TempColum = new Image2D(this.spritebatch, this.camera);
                TempColum.MyTexture2D = this.MyTexture2D;
                TempColum.MyViewData.Source = new Rectangle(0, 0, 1, 1);
                TempColum.MyViewData.Position = new Vector2(StartPositionX + (i * LangthWidth), 0);
                TempColum.MyViewData.Colors = myColorGrid;
                TempColum.MyViewData.Depth = myDepth;
                Colums.Add(TempColum);
            }
            #endregion 

            #region New Rows
            Rows.Clear();

            int LoopRow = (myArea.Height / LangthHeight) + 2;
            int newPositionY = (int)((this.camera.MyViewData.Position.Y * -1)/ LangthHeight);
            int StartPositionY = newPositionY * LangthHeight;
            for (int i = 0; i < LoopRow; i++)
            {
                Image2D TempRow = new Image2D(this.spritebatch, this.camera);
                TempRow.MyTexture2D = this.MyTexture2D;
                TempRow.MyViewData.Source = new Rectangle(0, 0, 1, 1);
                TempRow.MyViewData.Position = new Vector2(0, StartPositionY + (i * LangthHeight));
                TempRow.MyViewData.Colors = myColorGrid;
                TempRow.MyViewData.Depth = myDepth;
                Rows.Add(TempRow);
            }
            #endregion

            #region Update
            foreach (Image2D imageC in Colums)
            {
                imageC.MyViewData.Scale = new Vector2(1, myArea.Height);
                imageC.MyViewData.Position = new Vector2(imageC.MyViewData.Position.X, this.camera.MyViewData.Position.Y * -1);
            }

            foreach (Image2D imageR in Rows)
            {
                imageR.MyViewData.Scale = new Vector2(myArea.Width, 1);
                imageR.MyViewData.Position = new Vector2(this.camera.MyViewData.Position.X * -1, imageR.MyViewData.Position.Y);
            }
            #endregion
        }

        protected override void SubDraw()
        {
            base.SubDraw();

            foreach (Image2D imageC in Colums)
                imageC.Draw();

            foreach (Image2D imageR in Rows)
                imageR.Draw();
        }
    }
}
